﻿
using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Forms;
using Omniscient.Foundation.ApplicationModel;
using Omniscient.Foundation.ApplicationModel.Presentation;
using Omniscient.Foundation.Data;
using Omniscient.TimeTracker.Data;
using System.Drawing;

namespace Omniscient.TimeTracker.Presentation
{
    /// <summary>
    /// Show Hierarchical view of Clients, projects and tasks
    /// </summary>
    public partial class ClientProjectTaskView : System.Windows.Controls.UserControl, IView
    {
        ClientProjectTaskModel _model;

        System.Windows.Controls.ContextMenu Menu { get; set; }
        NotifyIcon MenuIcon { get; set; }
        Icon StartIcon { get; set; }
        Icon StopIcon { get; set; }

        public ClientProjectTaskView()
        {
            InitializeComponent();
            _model = new ClientProjectTaskModel(GetClients());
        }

       #region IView Members

        public IModel Model
        {
            get
            {
                return _model;
            }
            set
            {
                _model = (ClientProjectTaskModel)value;
            }
        }

        public void UpdateView()
        {
            if (Menu != null)
            {

            }
        }
        #endregion

        private EntityList<Client> GetClients()
        {
            //todo: refactor this code using different IComposer classes
            IEntityAdapter<Task> taskAdapter;
            IEntityAdapter<Project> projectAdapter;
            IEntityAdapter<Client> clientAdapter;

            taskAdapter = ApplicationManager.Current.ServiceProvider.GetService<IEntityAdapter<Task>>();
            projectAdapter = ApplicationManager.Current.ServiceProvider.GetService<IEntityAdapter<Project>>();
            clientAdapter = ApplicationManager.Current.ServiceProvider.GetService<IEntityAdapter<Client>>();

            if (taskAdapter == null || projectAdapter == null || clientAdapter == null)
            {
                return null;
            }

            EntityList<Task> taskList = taskAdapter.LoadAll();
            EntityList<Project> projectList = projectAdapter.LoadAll();
            EntityList<Client> clientList = clientAdapter.LoadAll();

            // Should be included in ClientComposer
            Dictionary<Guid, Client> clientDictionary;
            clientDictionary = new Dictionary<Guid, Client>();
            foreach (Client c in clientList)
            {
                clientDictionary.Add(c.Id, c);
            }

            // Should be included in ProjectComposer
            Dictionary<Guid, Project> projectDictionary;
            projectDictionary = new Dictionary<Guid, Project>();
            foreach (Project p in projectList)
            {
                projectDictionary.Add(p.Id, p);
            }

            // Should be included in ProjectComposer
            foreach (Task t in taskList)
            {
                Guid foreignKey = t.ParentProject.Id;

                if (projectDictionary.ContainsKey(foreignKey))
                {
                    t.ParentProject = projectDictionary[foreignKey];
                    projectDictionary[foreignKey].Tasks.Add(t);
                }
            }

            // Should be included in ClientComposer
            foreach (Project p in projectList)
            {
                Guid foreignKey = p.ParentClient.Id;

                if (clientDictionary.ContainsKey(foreignKey))
                {
                    p.ParentClient = clientDictionary[foreignKey];
                    clientDictionary[foreignKey].Projects.Add(p);
                }
            }

            // Should be included in ClientComposer
            EntityList<Client> result = new EntityList<Client>();
            foreach (Client client in clientDictionary.Values)
            {
                result.Add(client);
            }
            return result;
        }

        private EntityList<Category> GetCategories()
        {
            IEntityAdapter<Category> categoryAdapter;
            categoryAdapter = ApplicationManager.Current.ServiceProvider.GetService<IEntityAdapter<Category>>();

            if (categoryAdapter != null)
            {
                return categoryAdapter.LoadAll();
            }
            return null;
        }
    }
}
